package com.btl.service.ai.support.input.common.split.strategy;

import dev.langchain4j.data.document.DocumentSplitter;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class SplitStrategyFactory {

    private static final Map<SplitterEnum, SplitStrategy> strategyMap = new ConcurrentHashMap<>();

    @Resource
    private ApplicationContext applicationContext;


    @PostConstruct
    void init() {
        Collection<SplitStrategy> values = applicationContext.getBeansOfType(SplitStrategy.class)
                .values();
        for (SplitStrategy value : values) {
            strategyMap.put(value.splitter(), value);
        }
    }

    public static DocumentSplitter getDocumentSplitter(SplitterEnum splitterEnum) {
        return strategyMap.getOrDefault(splitterEnum, strategyMap.get(SplitterEnum.TITLED_RECURSIVE_SPLITTER))
                .getDocumentSplitter();
    }
}
